package com.tools.common.aop.inputcheck;

import android.util.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
public class InputCheckAspect {
    @Pointcut("execution(@com.tools.common.aop.inputcheck.CheckInput * *..*.*(..))")
    public void pcInputCheck() {}

    @Around("pcInputCheck()")
    public void inputCheckAspect(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Annotation[][] anno = method.getParameterAnnotations();
        Annotation thisAnno = null;
        Object[] args = joinPoint.getArgs();

        boolean valid = true;
        for(int i = 0; i < anno.length; i++) {
            for(int j = 0; j < anno[i].length; j++) {
                thisAnno = anno[i][j];
                Log.d("InputCheckAspect", thisAnno.toString());
                if(thisAnno instanceof Email) {
                    Log.d("InputCheckAspect", "email -> " + args[i]);
                } else if(thisAnno instanceof Password) {
                    Log.d("InputCheckAspect", "password -> " + args[i]);
                }
            }
        }

        if(valid) {
            try {
                joinPoint.proceed(args);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}
